<template>
  <el-dialog :title="isAudit?'审核货权控制':'查看货权控制'"
             :visible.sync="dialogForm"
             width="1200px">
    <el-form :inline="true"
             ref="form"
             :model="form"
             class="add-form"
             label-width="110px">
      <el-row>
        <el-col :span="8"
                class="el-height"><span class="labels">货主单位:</span>{{form.cargoOwnerName}}</el-col>
        <el-col :span="8"
                class="el-height"><span class="labels">所属仓库:</span>{{form.warehouseName}}</el-col>
      </el-row>
      <hr />
      <h3>货权控制信息</h3>
      <el-row>
        <el-col :span="8"
                class="el-height"><span class="labels">控制代码:</span>{{form.controlCode}}</el-col>
        <el-col :span="8"
                class="el-height"><span class="labels">控制单位:</span>{{form.controlOrgName}}</el-col>
        <el-col :span="8"
                class="el-height"><span class="labels">控制凭证号:</span>{{form.receiptsNo}}</el-col>
        <el-col :span="8"
                class="el-height"><span class="labels">控制类型:</span>{{form.controlType | _filterControlType}}</el-col>
        <el-col :span="8"
                class="el-height"><span class="labels">控制方式:</span>{{form.controlWay | _filterControlWay}}</el-col>
        <el-col :span="8"
                class="el-height"><span class="labels">备注:</span>{{form.remark}}</el-col>
      </el-row>
      <hr />
      <h3>控制量</h3>
      <el-row>
        <el-col :span="8"
                class="el-height"><span class="labels">质押量(吨):</span>{{form.pledgedWeight}}</el-col>
        <el-col :span="8"
                class="el-height"><span class="labels">放货量(吨):</span>{{form.releasedWeight}}</el-col>
        <el-col :span="8"
                v-show="show"
                class="el-height"><span class="labels">可用量:</span>{{form.totalUseWeight}}</el-col>
      </el-row>
      <el-row v-show="show">
        <el-col :span="8"
                class="el-height"><span class="labels">实际控制量/结存量:</span>{{form.factControlWeight}}</el-col>
        <el-col :span="8"
                class="el-height"><span class="labels">总质押量:</span>{{form.totalReleasedWeight}}</el-col>
        <el-col :span="8"
                class="el-height"><span class="labels">总放货量:</span>{{form.totalPledgedWeight}}</el-col>
        <el-col :span="8"
                class="el-height"><span class="labels">备注:</span>{{form.checkRemark}}</el-col>
      </el-row>
      <hr />
      <h3>被控制账页</h3>
      <el-table :data="form.pageList"
                border
                stripe
                show-summary
                :summary-method="getSummaries"
                max-height="550">
        <el-table-column label="序号"
                         type="index"
                         width="50"></el-table-column>
        <el-table-column prop="cargoOwnerName"
                         label="货主单位"
                         width="188"></el-table-column>
        <el-table-column prop="accountpageNo"
                         label="账页号"
                         width="120"></el-table-column>
        <el-table-column prop="accountPageType"
                         label="账页类型"
                         width="80">
          <template slot-scope="scope">
            {{scope.row.accountPageType | _filterAccountPageType}}
          </template>
        </el-table-column>
        <el-table-column prop="cargoName"
                         label="品名"
                         width="80"></el-table-column>
        <el-table-column prop="normStr"
                         label="规格"
                         width="80"></el-table-column>
        <el-table-column prop="material"
                         label="材质"
                         width="80"></el-table-column>
        <el-table-column prop="madeBy"
                         label="产地"
                         width="80"></el-table-column>
        <el-table-column prop="warehouseName"
                         label="所属仓库"
                         width="80"></el-table-column>
        <el-table-column prop="balanceWeight"
                         label="结存重量"
                         width="80"></el-table-column>
        <el-table-column prop="openedWeight"
                         label="开票重量"
                         width="80"></el-table-column>
        <el-table-column prop="outWeight"
                         label="出库重量"
                         width="80"></el-table-column>
        <el-table-column prop="balanceNum"
                         label="结存件数"
                         width="80"></el-table-column>
      </el-table>
      <div v-show="show2">
        <br />
        <hr />
        <h3>被退回原因</h3>
        <div>{{ form.failedReason }}</div>
      </div>
    </el-form>
    <div slot="footer"
         class="dialog-footer">
      <el-button v-show="show3"
                 @click="auditPass"
                 type="primary">审核通过</el-button>
      <el-button v-show="show3"
                 @click="auditFailed"
                 type="danger">审核不通过</el-button>
      <el-button @click="dialogForm = false">返 回</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { audit } from '@/api/things/auditList'
export default {
  name: 'auditPropertyControlForm',
  props: ['id'],
  data() {
    return {
      form: this.clearForm(),
      dialogForm: false,
      isAudit: false,
      show: false,
      show2: false,
      show3:false,
      data: []
    }
  },
  methods: {
    clearForm() {
      return {
        addOrReduce: '2',
        failedReason: '' //审核失败的原因
      }
    },
             //合计
    getSummaries(param) {
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if(index === 0) {
          sums[index] = '合计'
          return;
        }
        const values = data.map(item => Number(item[column.property]))
        if (
          column.property === 'balanceWeight' ||
          column.property === 'openedWeight' ||
          column.property === 'outWeight' ||
          column.property === 'balanceNum'
        ) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr)
            if (!isNaN(value)) {
              return prev + curr
            } else {
              return prev
            }
          }, 0)
          sums[index]
        } else {
          sums[index] = ''
        }
      })
      return sums
    },
    //审核通过
    auditPass() {
      audit(this.id, 1, { failedReason: '' }).then(res => {
        this.$message({ type: 'success', message: '审核通过' })
        this.$emit('load')
        this.dialogForm = false
      })
    },
    //审核不通过
    auditFailed() {
      this.$prompt('请输入不通过的原因', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      })
        .then(({ value }) => {
          audit(this.id, 2, { failedReason: value }).then(res => {
            this.form.failedReason = value
            this.$message({ type: 'warning', message: '审核不通过' })
            this.$emit('load', value)
            this.dialogForm = false
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '取消输入'
          })
        })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.el-height {
  margin: 10px 0;
}
h3 {
  margin: 10px 0;
}
.labels {
  margin-right: 5px;
}
</style>
